package twitter

//go:generate sqlboiler --no-hooks psql

import (
	"sync"

	"github.com/botlabs-gg/yagpdb/v2/common"
	"github.com/botlabs-gg/yagpdb/v2/common/config"
	"github.com/botlabs-gg/yagpdb/v2/common/mqueue"
	"github.com/botlabs-gg/yagpdb/v2/twitter/models"
	twitterscraper "github.com/n0madic/twitter-scraper"
)

var (
	logger = common.GetPluginLogger(&Plugin{})

	confTwitterBatchSize     = config.RegisterOption("yagpdb.twitter.batch_size", "Batch Size for scraping feeds", 50)
	confTwitterPollFrequency = config.RegisterOption("yagpdb.twitter.poll_frequency", "Minimum Delay in each feed poll for all feeds in minutes", 1)
	confTwitterBatchDelay    = config.RegisterOption("yagpdb.twitter.batch_delay", "Delay in seconds between each batch", 10)
)

type Plugin struct {
	Stop chan *sync.WaitGroup

	feeds          []*models.TwitterFeed
	feedsLock      sync.Mutex
	twitterScraper *twitterscraper.Scraper
}

func (p *Plugin) PluginInfo() *common.PluginInfo {
	return &common.PluginInfo{
		Name:     "Twitter",
		SysName:  "twitter",
		Category: common.PluginCategoryFeeds,
	}
}

func RegisterPlugin() {
	twitterScraper := twitterscraper.New()
	proxy := common.ConfHttpProxy.GetString()
	if len(proxy) > 0 {
		twitterScraper.SetProxy(proxy)
	}

	twitterScraper.LoginOpenAccount()
	p := &Plugin{
		twitterScraper: twitterScraper,
	}

	common.RegisterPlugin(p)
	mqueue.RegisterSource("twitter", p)
	common.InitSchemas("twitter", DBSchemas...)
}

const TwitterIcon = ``

func (p *Plugin) WebhookAvatar() string {
	return TwitterIcon
}
